<template>
  <el-dialog
    v-model="dialogShow"
    :close-on-click-modal="false"
    :destroy-on-close="false"
    custom-class="mydialog"
    width="60%"
    :title="titleContent"
    @close="close"
  >
    <el-form
      ref="formRef"
      class="myForm"
      :model="formData"
      :inline="true"
      :label-width="formLabelWidth"
      size="small"
      :rules="formRules"
    >
      <el-form-item
        label="产品信息："
        style="width: 100%;"
      >
        <div class="product-name">{{ getProductName(productDetail) }}</div>
      </el-form-item>
      <el-form-item
        label="价格编号："
        style="width: 47%; margin-right: 0px;"
      >
        <div>{{ priceDetail.priceNumber }}</div>
      </el-form-item>
      <el-form-item
        class="form-item"
        label="准入模式："
        prop="accessMode"
        style="width: 47%; margin-right: 0px;"
      >
        <div>{{ getLabel('access_mode', priceDetail.accessMode) }}</div>
      </el-form-item>
      <el-form-item
        class="form-item"
        label="适用地区："
        prop="region"
        style="width: 47%; margin-right: 0px;"
      >
        <area-list-view :areas="priceDetail.region" />
      </el-form-item>
      <el-form-item
        class="form-item"
        label="适用终端："
        prop="terminalType"
        style="width: 47%; margin-right: 0px;"
      >
        <div>{{ getLabel('app_mode',priceDetail.terminalType) }}</div>
      </el-form-item>
      <el-form-item
        class="form-item"
        label="终端价："
        prop="terminalPrice"
        style="width: 47%; margin-right: 0px;"
      >
        <div class="scj-form-item">
          <div><span>{{ priceDetail.terminalPrice }}</span><span>元</span><span v-if="productDetail.sellUnit"> / {{ getLabel('sell_unit', productDetail.sellUnit) }}</span></div>
        </div>
      </el-form-item>
      <el-form-item
        class="form-item"
        label="指导交易价："
        style="width: 47%; margin-right: 0px;"
      >
        <div class="scj-form-item">
          <div><span>{{ priceDetail.guidingPrice }}</span><span>元</span><span v-if="productDetail.sellUnit"> / {{ getLabel('sell_unit', productDetail.sellUnit) }}</span></div>
        </div>
      </el-form-item>
      <el-form-item
        label="作废原因："
        prop="banReason"
        style="width: 100%;margin-right: 0px;"
      >
        <el-input
          v-model="formData.banReason"
          type="textarea"
          style="width: 100%"
          clearable
          autosize
          placeholder="请输入作废原因"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div
        class="dialog-footer"
        style="display:flex;justify-content:center;align-items:center;"
      >
        <el-button
          class="border-btn"
          size="small"
          style="margin-right: 20px;"
          @click="close"
        >取消</el-button>
        <el-button
          class="main-btn"
          size="small"
          type="primary"
          @click="confirm"
        >提交审核</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {defineComponent, getCurrentInstance, inject, reactive, ref, toRefs,} from 'vue'

export default defineComponent({
  name: 'ObsoletePrice',
  emits: ['obsoleteSuccess'],
  setup(props, context) {
    const currentInstance = getCurrentInstance()
    const api = inject('api')
    const formRef = ref(null)
    var titleContent = ref('作废产品价格')
    var detailData = reactive({
      formData: {
        id: 0,
        banReason: '',
      },
      productDetail: {},
      priceDetail: {},
    })
    var formRules = reactive({
      banReason: [
        { required: true, message: '请输入作废原因', trigger: 'blur' },
      ],
    })
    var formLabelWidth = ref(120)
    var dialogShow = ref(false)
    const open = (detail) => {
      detailData.productDetail = detail.productDetail
      detailData.priceDetail = detail.priceDetail
      detailData.formData.id = detail.priceDetail.id

      dialogShow.value = true
    }
    const close = () => {
      dialogShow.value = false
      formRef.value.resetFields()
    }
    const obsoletePriceOperation = (formData) => {
      api.product.OBSOLETE_PRODUCT_PRICE(formData).then((res) => {
        if (res.code === 0) {
          context.emit('obsoleteSuccess')
          close()
        } else {
          currentInstance.proxy.customMessage({
            type: 'error',
            message: res.msg,
          })
        }
      })
    }
    const confirm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          obsoletePriceOperation(detailData.formData)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }

    return {
      formRef,
      titleContent,
      ...toRefs(detailData),
      formRules,
      formLabelWidth,
      dialogShow,
      open,
      close,
      confirm,
    }
  },
})
</script>

  <style lang="scss" scoped>
.myForm {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  .form-item {
    width: 47%;
  }
}
</style>

